<?php

namespace catchAdmin\community\controller;

use app\common\business\Message;
use catchAdmin\community\request\CommunityRequest;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\PushMessage as PushMessageModel;
use catcher\exceptions\FailedException;
use think\exception\ValidateException;

class Community extends CatchController
{
    protected ?\app\common\model\Community $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\Community();
    }


    public function index(CatchRequest $request): \think\response\Json
    {

        return CatchResponse::paginate($this->model->getLists());
    }

    public function save(CatchRequest $request): \think\response\Json
    {
        try {
            $data = $request->param();
            validate(CommunityRequest::class)->check($data);
            return CatchResponse::success($this->model->save($request->param()));

        } catch (ValidateException $exception) {
            throw new ValidateException($exception->getMessage());
        }
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        $result = $this->model->find($id);
        if (empty($result)) {
            throw new FailedException("数据不存在");
        }
        return CatchResponse::success($result->save($request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        $result = $this->model->find($id);
        if (empty($result)) {
            throw new FailedException("数据不存在");
        }
        return CatchResponse::success($result->destroy($id));
    }
}